using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Sublimation
{
    public class ScriptedEventKamikazee : ScriptedEvent
    {
        public ScriptedEventKamikazee(WorldProperties worldProperties)
            : base(worldProperties)
        {
        }

        protected const double SCRIPT_START_TIME_SECONDS = 230;

        protected const int APPEAR_NUMBER_OF_FRAMES_TO_PLAY = 8;
        protected const int APPEAR_FRAMES_PER_SECOND = 6;
        protected const int TOTAL_KAMIKAZEES_PER_WAVE = 14;
        Vector2[] spawnPosition = new Vector2[TOTAL_KAMIKAZEES_PER_WAVE];
        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            // 3:50 - kamikazees
            if (state == 0)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS) state = 1;

            }
            else if (state == 1)
            {
                // write the text warning of the approach
                worldProperties.DrawTextWarning("Tenno Heika Banzai!", ScriptedEvent.DEFAULT_WARNING_TEXT_MILLIS);
                state = 2;

            }
            else if (state == 2)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 1.5) state = 3;

            }
            else if (state == 3)
            {
                // show the appearing animation/effect for first wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    spawnPosition[i] = worldProperties.spawnPoints[worldProperties.Random.Next(WorldProperties.MAX_SPAWN_POINTS)].Position
                        + (float)worldProperties.Random.NextDouble() * SpawnPoint.MAX_ENEMY_SPAWN_DISTANCE * new Vector2((float)Math.Cos(worldProperties.Random.NextDouble() * MathHelper.TwoPi), (float)Math.Sin(worldProperties.Random.NextDouble() * MathHelper.TwoPi));
                    // play the teleport animation
                    new Animation(worldProperties,
                        spawnPosition[i], 0, new Vector2(90, 90), 1.0f,
                        APPEAR_NUMBER_OF_FRAMES_TO_PLAY, APPEAR_FRAMES_PER_SECOND, "Sprites\\scripted_kamikaze_appear");
                }

                state = 4;

            }
            else if (state == 4)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 2) state = 5;

            }
            else if (state == 5)
            {
                // spawn first wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    // spawn the unit
                    EnemyUnitKamikaze unitKamikaze = new EnemyUnitKamikaze(worldProperties, EnemyKind.Kamikaze);
                    // give it a spawn position
                    unitKamikaze.Position = spawnPosition[i];
                    // set the rotation to face them in towards the map
                    Vector2 vectorToCenter = new Vector2(worldProperties.Map.WorldBounds.Center.X, worldProperties.Map.WorldBounds.Center.Y) - unitKamikaze.Position;
                    unitKamikaze.Rotation = (float)Math.Atan2(vectorToCenter.Y, vectorToCenter.X);
                    worldProperties.Enemies.Add(unitKamikaze);
                }

                state = 6;
            }
            else if (state == 6)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 12) state = 7;
            }
            else if (state == 7)
            {
                // write the text warning of the approach
                worldProperties.DrawTextWarning("Tenno Heika Banzai!", ScriptedEvent.DEFAULT_WARNING_TEXT_MILLIS);
                state = 8;
            }
            else if (state == 8)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 13.5) state = 9;
                
            }
            else if (state == 9)
            {
                // show the appearing animation/effect for second wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    spawnPosition[i] = worldProperties.spawnPoints[worldProperties.Random.Next(WorldProperties.MAX_SPAWN_POINTS)].Position
                        + (float)worldProperties.Random.NextDouble() * SpawnPoint.MAX_ENEMY_SPAWN_DISTANCE * new Vector2((float)Math.Cos(worldProperties.Random.NextDouble() * MathHelper.TwoPi), (float)Math.Sin(worldProperties.Random.NextDouble() * MathHelper.TwoPi));
                    // play the teleport animation
                    new Animation(worldProperties,
                        spawnPosition[i], 0, new Vector2(90, 90), 1.0f,
                        APPEAR_NUMBER_OF_FRAMES_TO_PLAY, APPEAR_FRAMES_PER_SECOND, "Sprites\\scripted_kamikaze_appear");
                }

                state = 10;
            }
            else if (state == 10)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 14) state = 11;
            }
            else if (state == 11)
            {
                // spawn second wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    // spawn the unit
                    EnemyUnitKamikaze unitKamikaze = new EnemyUnitKamikaze(worldProperties, EnemyKind.Kamikaze);
                    // give it a spawn position
                    unitKamikaze.Position = spawnPosition[i];
                    // set the rotation to face them in towards the map
                    Vector2 vectorToCenter = new Vector2(worldProperties.Map.WorldBounds.Center.X, worldProperties.Map.WorldBounds.Center.Y) - unitKamikaze.Position;
                    unitKamikaze.Rotation = (float)Math.Atan2(vectorToCenter.Y, vectorToCenter.X);
                    worldProperties.Enemies.Add(unitKamikaze);
                }

                state = 12;
            }
            else if (state == 12)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 24) state = 13;
            }
            else if (state == 13)
            {
                // write the text warning of the approach
                worldProperties.DrawTextWarning("Tenno Heika Banzai!", ScriptedEvent.DEFAULT_WARNING_TEXT_MILLIS);
                state = 14;
            }
            else if (state == 14)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 25) state = 15;

            }
            else if (state == 15)
            {
                // show the appearing animation/effect for third wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    spawnPosition[i] = worldProperties.spawnPoints[worldProperties.Random.Next(WorldProperties.MAX_SPAWN_POINTS)].Position
                        + (float)worldProperties.Random.NextDouble() * SpawnPoint.MAX_ENEMY_SPAWN_DISTANCE * new Vector2((float)Math.Cos(worldProperties.Random.NextDouble() * MathHelper.TwoPi), (float)Math.Sin(worldProperties.Random.NextDouble() * MathHelper.TwoPi));
                    // play the teleport animation
                    new Animation(worldProperties,
                        spawnPosition[i], 0, new Vector2(90, 90), 1.0f,
                        APPEAR_NUMBER_OF_FRAMES_TO_PLAY, APPEAR_FRAMES_PER_SECOND, "Sprites\\scripted_kamikaze_appear");
                }

                state = 16;
            }
            else if (state == 16)
            {
                if (gameTime.TotalGameTime.Subtract(worldProperties.PauseTime).TotalSeconds >= SCRIPT_START_TIME_SECONDS + 25.5) state = 17;
            }
            else if (state == 17)
            {
                // spawn third wave
                for (int i = 0; i < TOTAL_KAMIKAZEES_PER_WAVE; i++)
                {
                    // spawn the unit
                    EnemyUnitKamikaze unitKamikaze = new EnemyUnitKamikaze(worldProperties, EnemyKind.Kamikaze);
                    // give it a spawn position
                    unitKamikaze.Position = spawnPosition[i];
                    // set the rotation to face them in towards the map
                    Vector2 vectorToCenter = new Vector2(worldProperties.Map.WorldBounds.Center.X, worldProperties.Map.WorldBounds.Center.Y) - unitKamikaze.Position;
                    unitKamikaze.Rotation = (float)Math.Atan2(vectorToCenter.Y, vectorToCenter.X);
                    worldProperties.Enemies.Add(unitKamikaze);
                }

                state = 18;
            }
            else if (state == 18)
            {
                Game.Components.Remove(this);
            }
        }
    }
}